package homework03;

/*模拟三个老师同时分发80份学习笔记，每个老师相当于一个线程。*/
public class Thread03 {
    public static void main(String[] args) {
        Teacher tc = new Teacher();
        /*将创建的一个任务封装到三个线程中*/
        Thread t1 = new Thread(tc, "老师1");
        t1.start();
        Thread t2 = new Thread(tc, "老师2");
        t2.start();
        Thread t3 = new Thread(tc, "老师3");
        t3.start();
    }
}

class Teacher implements Runnable {//实现Runnable接口来将任务封装
    Notes n = new Notes();

    public void run() {

        n.distribute();

    }
}

class Notes {
    private int notes_num = 80;
    private Object obj = new Object();//创建一个锁

    void distribute() {
        while (true) {
            synchronized (obj) {//利用同步达到线程间的互斥
                if (notes_num >= 0) {

                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                    }
                    System.out.println(Thread.currentThread().getName() + "剩余作业.........." + notes_num--);
                } else {
                    break;
                }
            }
        }
    }
}

